package indi.h.devmg.acquisition.snmp.mib.rules;

import java.util.ArrayList;
import java.util.List;

import indi.h.devmg.acquisition.snmp.mib.beans.MIB;
import indi.h.devmg.acquisition.snmp.mib.beans.Node;
import indi.h.devmg.acquisition.snmp.mib.util.ArrayFormat;

public class NodeRule implements Rule<Node> {

	private MIB mib;

	private Node node;

	private List<String> params;

	private String param;

	private boolean end;

	public NodeRule(MIB mib) {
		this(mib, false);
	}

	public NodeRule(MIB mib, boolean end) {
		this.mib = mib;
		this.end = end;
		this.params = new ArrayList<>();
		this.params.add("SYNTAX");
		this.params.add("MAX-ACCESS");
		this.params.add("STATUS");
		this.params.add("DESCRIPTION");
		this.params.add("::=");
		this.params.add("INDEX");
		this.params.add("REFERENCE");
		this.params.add("OBJECTS");
		this.params.add("MODULE");
		this.params.add("LAST-UPDATED");
		this.params.add("ORGANIZATION");
		this.params.add("CONTACT-INFO");
	}

	@Override
	public Node getTarget() {
		// TODO Auto-generated method stub
		return this.node;
	}

	@Override
	public int addData(String s) {
		// TODO Auto-generated method stub
		if (end)
			return 1;
		addParams(s.trim());
		return 0;
	}

	protected boolean addParams(String s) {
		int indexOf = s.indexOf(" ");
		if (indexOf > 0 && params.contains(s.substring(0, indexOf))) {
			String substring = s.substring(0, indexOf);
			param = substring;
			addParam(s.substring(indexOf + 1));
			return end;
		} else {
			if (params.contains(s)) {
				param = s;
				return false;
			} else {
				addParam(s);
				return end;
			}
		}
	}

	protected void addParam(String s) {
		if (param == null)
			return;
		if (param.equals("LAST-UPDATED")) {
			node.setLast_updated(s);
		} else if (param.equals("ORGANIZATION")) {
			node.setOrganization(node.getOrganization() == null ? s : node.getOrganization() + " " + s);
		} else if (param.equals("CONTACT-INFO")) {
			node.setContact_info(node.getContact_info() == null ? s : node.getContact_info() + " " + s);
		} else if (param.equals("SYNTAX")) {
			node.setSyntax(node.getSyntax() == null ? s : node.getSyntax() + " " + s);
		} else if (param.equals("MAX-ACCESS")) {
			node.setMax_access(s);
		} else if (param.equals("STATUS")) {
			node.setStatus(s);
		} else if (param.equals("DESCRIPTION")) {
			node.setDescription(node.getDescription() == null ? s : node.getDescription() + " " + s);
		} else if (param.equals("::=")) {
			List<String> split = ArrayFormat.removeEmpty(s.replaceAll("\\{|\\}", "").trim().split(" "));
			node.setParent(split.toString());
			param = null;
			end = true;
		} else if (param.equals("INDEX")) {
			node.setIndex(s.replaceAll("\\{|\\}", "").trim());
		} else if (param.equals("REFERENCE")) {
			node.setReference(node.getReference() == null ? s : node.getReference() + " " + s);
		} else if (param.equals("OBJECTS")) {
			node.setObjects(node.getObjects() == null ? s : node.getObjects() + " " + s);
		} else if (param.equals("MODULE")) {
			node.setModule(node.getModule() == null ? s : node.getModule() + " " + s);
		}
	}

	@Override
	public void init(String s) {
		// TODO Auto-generated method stub
		s = s.trim();
		List<String> split = ArrayFormat.removeEmpty(s.split(" "));
		if (split.size() < 2) {
			end = true;
			return;
		}

		this.node = new Node();
		this.node.setName(split.get(0));
		this.node.setType(split.get(1));
		//是否对大小写敏感
		//this.mib.getNodes().put(node.getName().trim().toLowerCase(), node);
		this.mib.getNodes().put(node.getName().trim(), node);

		int index = getIndex(split, "::=");

		if (index < 2)
			return;
		formatNode(split, index);

	}

	protected void formatNode(List<String> l, int index) {

		String type = "";
		for (int i = 1; i < index; i++) {
			type += " " + l.get(i);
		}

		this.node.setType(type);

		if (l.size() <= index + 1) {
			param = "::=";
			return;
		}

		if (!l.get(index + 1).startsWith("{"))
			return;

		String oid = "";

		for (int i = index + 1; i < l.size(); i++) {
			oid += " " + l.get(i);
			if (l.get(i).startsWith("}")) {
				break;
			}
		}
		oid.trim();

		if (!oid.endsWith("}"))
			return;

		String[] oids = oid.trim().replaceAll("\\{|\\}", "").trim().split(" ");

		node.setParent(ArrayFormat.removeEmpty(oids).toString());

		end = true;
	}

	protected int getIndex(List<String> l, String s) {
		for (int i = 0; i < l.size(); i++) {
			if (l.get(i).equals(s)) {
				return i;
			}
		}
		return -1;
	}

}
